class ClassLoader {

    constructor() {
        this.classTable = new Map();
        this.classPath = ".";
    }

    load(name) {
        let clazz = this.classTable.get(name.stringValue);

        if (clazz == null || clazz == undefined) {
            const url = window.location.origin + "/" + "mjvm" + "/" + name.stringValue + ".class";

            // console.log(url);
            clazz = new JD.ClassFile(new JD.Stream(this.retrieve(url)));

            //非object类
            if (clazz.superClass != undefined && clazz.superClass != 0) {
                //load super class
                const superClassInfo = clazz.constantPool[clazz.superClass];
                const nameIndex = clazz.constantPool[superClassInfo.nameIndex];
                const superClass = this.load(nameIndex);
            }

            this.classTable.set(name.stringValue, clazz);
            return clazz;
        }
        return clazz;
    }

    // load(stream) {
    //     return new JD.ClassFile(stream);
    // }

    retrieve(url) {
        var request = new XMLHttpRequest();
        request.open("GET", url, false);
        request.overrideMimeType("text/plain; charset=x-user-defined");
        request.send();
        if ((200 !== request.status) && (0 !== request.status)) {
            throw new Error(request.status + " Retrieving " + url);
        }
        return request.responseText;
    }

}